package com.sakila.action.payment;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sakila.action.BaseAction;
import com.sakila.pojo.Member;
import com.sakila.pojo.Payment;
import com.sakila.service.InventoryService;
import com.sakila.service.RentalService;
import com.sakila.vo.MyUser;

@Controller
public class RentalAction implements BaseAction {
	private static final Logger logger = LoggerFactory.getLogger(RentalAction.class);
	@Resource
	private InventoryService inventoryService;
	@Resource
	private RentalService rentalService;
	
	@RequestMapping(value="/staff/goto_addRental")
	public String viewRental(){
		return "staff/rental/add";
	}
	
	@RequestMapping(value="/staff/check/{memberId}")
	@ResponseBody
	public Object checkRentable(@PathVariable Integer memberId){
		Member member = new Member();
		member.setId(memberId);
		return rentalService.isCustomerRentable(member);
	}
	
	/**
	 *  If error occur, jump to "return_add" page which duplicates the add page to show the error message;
	 *  On the page, should keep the memberId and filmIds informations.
	 */
	@RequestMapping(value="/staff/addRental")
	public String rental(Model model, Authentication au, 
						@RequestParam("filmId") Integer[] filmIds, 
						@RequestParam Integer memberId){
		model.addAttribute("memberId", memberId);
		model.addAttribute("filmIds", filmIds);
		
		/**
		 * server side checking again due to ajax non-reliable
		 */
		Member member = new Member();
		member.setId(memberId);
		if(rentalService.isCustomerRentable(member) == false){
			model.addAttribute("error", "Customer has more than 3 unreturned orders");
			return "staff/rental/return_add";
		}
			
		if(filmIds.length == 0){
			model.addAttribute("error", "No film id input");
			return "staff/rental/return_add";
		}		
		if(filmIds.length > 3){
			logger.warn("suspicious operation: filmIds length greater than 3.");
			model.addAttribute("error", "No film id input");
			return "staff/rental/return_add";
		}
		
		MyUser user = (MyUser) au.getPrincipal();
		List<Payment> payments = new ArrayList<Payment>();
		int result = rentalService.addRental(filmIds, user.getStaff(), memberId, payments);
		if(result > 0){
			model.addAttribute("error", "Film Id + " + result + "not rentable.");
			return "staff/rental/return_add";
		}
		
		model.addAttribute("payments", payments);
		return "staff/rental/payment";
	}
	
	@RequestMapping(value={"/getTop5c", "/staff/getTop5c"})
	@ResponseBody
	public Object getTop5HotCategories(){
		return rentalService.getTop5HotCategories();
	}
	
	@RequestMapping(value={"/getTop10f", "/staff/getTop10f"})
	@ResponseBody
	public Object getTop10HotFilms(){
		return rentalService.getTop10HotFilms();
	}
}
